//
//  WH_HomeDetailHeard.m
//  App_6.23
//
//  Created by wanghao on 16/7/5.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_HomeDetailHeard.h"
#import "WH_Home.h"

#define LUNBOHEIGHT 300

@interface WH_HomeDetailHeard ()<UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIScrollView *bigScroll;
@property (nonatomic, strong) NSTimer *myTimer;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIPageControl *pageControl;


@end
@implementation WH_HomeDetailHeard

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        
    }
    
    return self;
    
}

- (void)createSubViews:(NSMutableArray *)images{
    
    
    if (images.count != 0){
        
        
        self.imageArray = [NSMutableArray arrayWithArray:images];
        id imageFirst = [self.imageArray firstObject];
        id imageLast = [self.imageArray lastObject];
        
        [self.imageArray insertObject:imageLast atIndex:0];
        [self.imageArray addObject:imageFirst];
        
        
        self.bigScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, LUNBOHEIGHT)];
        self.bigScroll.pagingEnabled = YES;
        self.bigScroll.bounces = NO;
        self.bigScroll.showsHorizontalScrollIndicator = NO;
        self.bigScroll.delegate = self;
        self.bigScroll.contentSize = CGSizeMake(SCREEN_WIDTH*self.imageArray.count, LUNBOHEIGHT);
        self.bigScroll.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        //轮播图
        for (int i = 0; i < self.imageArray.count; i++) {
            
            NSString *name = self.imageArray[i];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, LUNBOHEIGHT)];
            //第三方图片赋值
            [imageView sd_setImageWithURL:[NSURL URLWithString:name] placeholderImage:nil];
            
            [self.bigScroll addSubview:imageView];
            
        }
        [self addSubview:self.bigScroll];
        //创建小点
        self.pageControl = [[UIPageControl alloc] init];
        [self addSubview:self.pageControl];
        
        [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(self);
            make.top.equalTo(@(LUNBOHEIGHT));
            make.width.equalTo(@(SCREEN_WIDTH/2));
            make.height.equalTo(@20);
            
        }];
        self.pageControl.numberOfPages = [self.imageArray count] - 2;
        
        [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];

    }
}
-(void)createModel:(WH_Home *)model{
    
    self.nameLabel = [UILabel new];
    _nameLabel.text = model.product_name;
    _nameLabel.font = [UIFont systemFontOfSize:14.f];
    _nameLabel.numberOfLines = 2;
    [self addSubview:_nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@5);
        make.right.mas_equalTo(@-5);
        make.top.equalTo(@(LUNBOHEIGHT + 15));
        make.height.mas_equalTo(@40);
    }];
    self.periodLabel = [UILabel new];
    _periodLabel.text = [NSString stringWithFormat:@"期号: %@",model.product_period_no];
    _periodLabel.font = [UIFont systemFontOfSize:13.f];
    _periodLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:_periodLabel];
    [self.periodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        make.height.equalTo(@20);
    }];
    UILabel *label = [UILabel new];
    label.backgroundColor = [UIColor colorWithRed:0.94 green:0.93 blue:0.93 alpha:1];
    label.layer.cornerRadius = 3;
    label.layer.masksToBounds = YES;
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.nameLabel);
        make.top.equalTo(self.periodLabel.mas_bottom);
        make.height.equalTo(@10);
        
    }];
    NSInteger a = [model.product_total integerValue];
    NSInteger b = [model.product_count integerValue];
    _shengyuC = a - b;
    NSInteger x = SCREEN_WIDTH - 10;
    CGFloat r = (CGFloat)b/a*x;
    self.rateLabel = [UILabel new];
    _rateLabel.backgroundColor = [UIColor colorWithRed:0.99 green:0.78 blue:0.22 alpha:1];
    _rateLabel.layer.cornerRadius = 3;
    _rateLabel.layer.masksToBounds = YES;
    [self addSubview:_rateLabel];
    
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(label);
        make.top.height.equalTo(label);
        make.width.equalTo(@(r));
    }];
    
    self.totalLabel = [UILabel new];
    _totalLabel.text = [NSString stringWithFormat:@"总需%@人次",model.product_total];
    _totalLabel.textColor = [UIColor lightGrayColor];
    _totalLabel.font = [UIFont systemFontOfSize:13.f];
    [self addSubview:_totalLabel];
    [self.totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(label.mas_bottom);
        make.height.equalTo(@20);
        make.width.equalTo(@100);
    }];
    
    self.countLabel = [UILabel new];
    _countLabel.text = [NSString stringWithFormat:@"剩余%ld",(a - b)];
    _countLabel.textColor = [UIColor blueColor];
    _countLabel.textAlignment = NSTextAlignmentRight;
    _countLabel.font = [UIFont systemFontOfSize:13.f];
    [self addSubview:_countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.nameLabel);
        make.top.equalTo(label.mas_bottom);
        make.height.equalTo(@20);
        make.width.equalTo(@100);
    }];

    
}

//UIPageControl类型对象中调用的方法
- (void)changePage:(UIPageControl *)page
{
    [self.bigScroll setContentOffset:CGPointMake((page.currentPage + 1) * self.bigScroll.frame.size.width, 0) animated:YES];
}
//使页面上点的变化与视图的变化一致
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.pageControl.currentPage = (scrollView.contentOffset.x - self.bigScroll.frame.size.width) / scrollView.frame.size.width;
    
}

//定时器在不断循环滚动,就不会减速停止,此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.bigScroll.contentOffset.x >= self.bigScroll.frame.size.width * (self.imageArray.count - 1)) {
        
        NSLog(@"最后一页");//循环滚动
        [self.bigScroll setContentOffset:CGPointMake(self.bigScroll.frame.size.width, 0) animated:NO];
    }
    
    if (self.bigScroll.contentOffset.x <= 0) {
        
        [self.bigScroll setContentOffset:CGPointMake(self.bigScroll.frame.size.width * (self.imageArray.count - 2), 0) animated:NO];
    }
    
    //不管怎么移动,保证self.index存的是当前的页码
    self.index = self.bigScroll.contentOffset.x / self.bigScroll.frame.size.width -1;
    
}

@end
